import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast.js';
import { loginVerify, getProdOrderAll } from '../../api/home/index'
import utils from '../../utils/util'
// 获取应用实例
const app = getApp()

Page({
  data: {
    scrollTop: 0,
    mytabbar: '',
    imageStatus: '/images/icon/home/home-click.png',
    mineImage: '/images/icon/home/mine.png',
    tabStatus: 1,
    isSearch: false,
    value: '',
    dialogclass: 'dialog-position',
    show: false,
    showSubscribe: false,
    imgUrl: 'https://gitee.com/zhang-zhaoming/imgs/raw/master/yidashop/',
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    canIUseGetUserProfile: false,
    canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName') // 如需尝试获取用户信息可改为false
  },
  // 前往购票页面
  toTicketReversePage() {
    wx.navigateTo({
      url: '/pages/view/ticketReverse/ticketReverse',
    })
  },
  watchBack() {},
  // 选择切换tab
  chooseTab(e) {
    this.setData({
      tabStatus: parseInt(e.currentTarget.dataset.index)
    })
  },
  // 前往签到页面
  toSignPage() {
    wx.navigateTo({
      url: '/pages/view/sign/sign',
    })
  },
  // 前往校园页面
  toSchoolPage() {
    console.log('前往校园页面')
  },
  // 前往专区页面
  toSpecialAreaPage(e) {
    wx.navigateTo({
      url: '/pages/view/specialArea/specialPage?pageid=' + e.currentTarget.dataset.id,
    })
  },
  // 前往一周热抢页面
  toWeekShopPage() {
    wx.navigateTo({
      url: '/pages/view/weekShop/weekShop',
    })
  },
  // 前往分享赚钱页面
  toSharePage() {
    wx.navigateTo({
      url: '/pages/view/sharePage/sharePage',
    })
  },
  // 前往商家入驻页面
  toMerchantInPage() {
    wx.navigateTo({
      url: '/pages/view/merchantIn/merchantIn',
    })
  },
  onClickImage() {
    const that = this
    that.onClickHide()
    wx.previewImage({
      // urls: ['http://imgweb.kejipro.cn/p3037/t4/201807/ec494985-726c-4ce9-9186-e7ba0830a0c0.jpg']
      urls: ['https://gitee.com/zhang-zhaoming/imgs/raw/master/yidashop/wechat-ercode.jpg']
    })
  },
  onCancel(e) {
    console.log(e)
  },
  onChange(e) {
    this.setData({
      value: e.detail,
    });
  },
  onSearch() {
    Toast('搜索' + this.data.value);
  },
  onClick() {
    Toast('搜索' + this.data.value);
  },
  onClickShow() {
    console.log('显示')
    this.setData({
      show: true
    });
  },

  onClickHide() {
    this.setData({
      show: false,
      showSubscribe: false
    });
  },

  // 订阅消息
  subscribeNews() {
    Toast.success('订阅成功')
    this.setData({
      showSubscribe: false
    });
  },

  noop() {},
  totestpage() {
    wx.navigateTo({
      url: '/pages/view/testPage/testPage',
    })
  },
  tochoosepage(e) {
    console.log(e)
    wx.navigateTo({
      url: '/pages/view/tabChoose/tabChoose?activeIndex=' + e.currentTarget.dataset.page,
    })
  },
  tochoosepageSearch(e) {
    console.log(e)
    wx.navigateTo({
      url: '/pages/view/tabChoose/tabChoose?activeIndex=' + e.currentTarget.dataset.page + '&search=' + e.currentTarget.dataset.search,
    })
  },
  getOrderList() {
    const that = this
    console.log('进行订单列表请求', this.data.myOrderState)
    getProdOrderAll({
      orderStatus: '',
      userId: 'ol0-S5fIxoO9irdNEKkgGw7ae9kQ'
      // userId: wx.getStorageSync('openid')
    }).then(res => {
      console.log(res)
      res.data.list.forEach(value => {
        value.createDate = value.createDate.split(' ')[0]
      })
      that.setData({
        orderList: res.data.list,
        linkCardNo: res.data.list.length > 0 ? res.data.list[0].linkCardNo : ''
      })
    }).catch(error => {
      console.log(error)
      if(error.code === 500) {
        that.setData({
          orderList: []
        })
      }
    })
  },
  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  //监听屏幕滚动 判断上下滚动 
  onPageScroll: function (ev) {
    var _this = this;
    // // 是的
    //当滚动的top值最大或者最小时，为什么要做这一步是由于在手机实测小程序的时候会发生滚动条回弹。防止发生回弹，设置默认最大最小值   
    if (ev.scrollTop <= 0) {
      ev.scrollTop = 0;
    } else if (ev.scrollTop > wx.getSystemInfoSync().windowHeight) {
      ev.scrollTop = wx.getSystemInfoSync().windowHeight;
      this.getTabBar().setData({
        isUp: true,
        imageStatus: '/images/icon/home/home-up.png',
        mineImage: '/images/icon/home/mine.png'
      })
    } else if (ev.scrollTop < wx.getSystemInfoSync().windowHeight) {
      this.getTabBar().setData({
        isUp: false,
        imageStatus: '/images/icon/home/home-click.png',
        mineImage: '/images/icon/home/mine.png'
      })
    }
    //给scrollTop重新赋值    
    setTimeout(function () {
      _this.setData({
        scrollTop: ev.scrollTop
      })
    }, 0)
  },
  onLoad() {
    wx.setStorageSync('image', 'click')
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: false, //  true时打开客服
        showSubscribe: false // true时打开提醒
      })
    }
    this.getOrderList()
  },
  onShow() {
    // wx.pageScrollTo({
    //   scrollTop:0
    // })
    // 设置tabBar显示中间的选项
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        imageStatus: this.getTabBar().data.isUp ? '/images/icon/home/home-up.png' : '/images/icon/home/home-click.png',
        mineImage: '/images/icon/home/mine.png'
      })
    }
    const time = new Date()
    console.log(utils.formatTime(time))
    console.log(utils.getUnixTime(utils.formatTime(time)))
    let stime = 1800000 - (parseInt(new Date().getTime()/1000) - utils.getUnixTime(utils.formatTime(time)))*1000 // 30分钟
    // console.log(stime)
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  getUserInfo(e) {
    // 不推荐使用getUserInfo获取用户信息，预计自2021年4月13日起，getUserInfo将不再弹出弹窗，并直接返回匿名的用户个人信息
    console.log(e)
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})